define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.customer.pageList(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"companyName",status:"auditStatus",statusName:"statusName",errMsg:"auditMsg",createTime:"createTime"
                        },true);
                        if(!$.ExtCommon.isCheckEmpty(newInfo.createTime)){newInfo.createTime=$.ExtMoment.initFormat(newInfo.createTime)}
                        var statusMap=servers.base.getMapCustomerStatus(newInfo.status.toString());
                        newInfo.statusName=statusMap.name;
                        newInfo.isSuccess=statusMap.idMap==="2";
                        newInfo.isFail=statusMap.idMap==="-1";
                        newInfo.isFrozen=statusMap.idMap==="3";
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger('bind:PageList');
            });
        },
        getAllList:function () {
            var self = this;
            servers.customer.list(function(res){
                var newData=[];
                if(res.success){
                    newData=$.map(res.data,function (info) {
                        var newInfo = $.ExtCommon.getMappingJson(info, {id: "id", name: "name"}, true);
                        newInfo.nameExt = newInfo.id + "_" + newInfo.name;
                        return newInfo;
                    });
                }
                res.data=newData;
                self.set({dataAllList: res});
                self.trigger('bind:AllList');
            });
        },
        getInfo: function (id) {
            var self = this;
            if (id) {
                servers.customer.info(id, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                        id: "id",name: "companyName",spreadUrl: "promotionUrl",industryType: "industryId",companyName: "corporateImageName",
                        licenseImgData: "businessLicense",icpImgData: "icpRecordChart",industryImgData: "industryQualificationImageList",adImgData: "adQualificationImageList",
                        mobile: "contactPersonMobile",tel: "contactPersonTelephone",commission:"rebate",campaignCount:"campaignCount"
                    }, true);
                    newJson.licenseImgData = $.ExtCommon.isCheckEmpty(newJson.licenseImgData)
                        ? {} : $.ExtCommon.getMappingJson(newJson.licenseImgData, {id: "imageId", url: "imageUrl"}, true);
                    newJson.licenseImg = newJson.licenseImgData.url;

                    newJson.icpImgData = $.ExtCommon.isCheckEmpty(newJson.icpImgData)
                        ? {} : $.ExtCommon.getMappingJson(newJson.icpImgData, {id: "imageId", url: "imageUrl"}, true);
                    newJson.icpImg = newJson.licenseImgData.url;

                    newJson.industryImgData = $.ExtCommon.isCheckEmpty(newJson.industryImgData)
                        ? [{url: ""}] : $.map(newJson.industryImgData, function (info) {
                            return $.ExtCommon.getMappingJson(info, {id: "imageId", url: "imageUrl"}, true)
                        });

                    newJson.adImgData = $.ExtCommon.isCheckEmpty(newJson.adImgData)
                        ? [{url: ""}] : $.map(newJson.adImgData, function (info) {
                            return $.ExtCommon.getMappingJson(info, {id: "imageId", url: "imageUrl"}, true)
                        });
                    res.dataOriginal = res.data;
                    res.data = newJson;
                    self.set({dataInfo: {isAdd: false, res: res}});
                    self.trigger('bind:Info');
                });
            }else {
                var dataDef={campaignCount:0,licenseImgData:{},icpImgData:{},industryImgData:[{url: ""}],adImgData:[{url: ""}]};
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        addInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            servers.customer.add(data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            data.id=json.id;
            servers.customer.update(data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        _getMappingToServer:function(json){
            var newJson = $.ExtCommon.getMappingJson(json, {
                companyName: "name",promotionUrl: "spreadUrl",industryId: "industryType",corporateImageName: "companyName",
                contactPersonMobile: "mobile",contactPersonTelephone: "tel", businessLicense: "licenseImgData.id",
                icpRecordChart: "icpImgData.id",audit:"audit",rebate:"commission"
            });
            if(!$.ExtCommon.isCheckEmpty(json.industryImgData)){ newJson.industryQualificationImageIdList=$.map(json.industryImgData,function (info) {return info.id})}
            if(!$.ExtCommon.isCheckEmpty(json.adImgData)){ newJson.adQualificationImageIdList=$.map(json.adImgData,function (info) {return info.id})}
            return newJson
        }
    });
});